import { By, until, Key } from "selenium-webdriver"
import { AutoDeployBlog } from "./autoDeployBlog"

export class JuejinAutoDeployBlog extends AutoDeployBlog {

    // private config: cookiesConfig

    constructor(cookies: string, blogContent: blogContent, config: cookiesConfig) {
        super(cookies, blogContent)

        this.blogContent = blogContent
        // this.config = config
        this.homeUrl = config.homeUrl
        this.mdUrl = config.mdUrl
        this.domain = config.domain
        this.httpOnlyCookies = config.httpOnlyCookies
        this.secureCookies = config.secureCookies
        this.xpath = config.xpath
    }

    async deploy() {
        await this.driver.get(this.homeUrl)
        await this.cookiesLogin()
        await this.driver.get(this.mdUrl)
        
        this.driver.findElement(By.xpath(<string>this.xpath.contentXPath)).sendKeys(Key.CONTROL, 'a', Key.DELETE)
        this.driver.findElement(By.xpath(this.xpath.titleXpath)).sendKeys(Key.CONTROL, 'a', Key.DELETE)
        // 读取MD文件内容
        let { mdContent, titleObj } = this.blogContent
        // 标题，内容从文件里面取
        await this.driver.findElement(By.xpath(this.xpath.contentXPath)).sendKeys(mdContent.join('\n'))
        await this.driver.findElement(By.xpath(this.xpath.titleXpath)).sendKeys(titleObj.title)
        // 进行发布文章配置
        await this.driver.sleep(2000)
        this.driver.findElement(By.xpath(this.xpath.publishButtonXPath)).click()
        await this.driver.wait(until.elementLocated(By.xpath(this.xpath.publishModalXPath)))
        // 添加标签
        await this.driver.findElement(By.xpath(this.xpath.tagXpath)).sendKeys('JavaScript', Key.ENTER)
        // 添加类型
        await this.driver.findElement(By.xpath(this.xpath.typeXpath)).sendKeys('JavaScript', Key.ENTER)
        // 添加分类
        await this.driver.findElement(By.xpath(this.xpath.frontEndXpath)).click()
        // 发布文章
        await this.driver.findElement(By.xpath(this.xpath.publishSubmitXpath)).click()
        // 得到地址
        let blogUrlEle = await this.driver.wait(until.elementLocated(By.xpath(this.xpath.blogUrlXpath)))
        let blogUrl = await blogUrlEle.getAttribute('href')
        console.log('blogUrl :>> ', blogUrl);
        console.log('END!!!')
        return blogUrl
    }

}